from kafka import KafkaProducer, KafkaConsumer
import json

# Kafka 服务器地址和端口号
# bootstrap_servers = ['192.168.10.130:9092', '10.0.10.3:9092']
# bootstrap_servers = ['192.168.10.130:9092']
bootstrap_servers = ['192.168.10.130:9092']
# bootstrap_servers = ['10.0.10.13:9092']
# bootstrap_servers = ['10.0.0.3:9092']
print(bootstrap_servers)
# Kafka 主题名称
topic = 'already_product_topic_2'
# 创建 Kafka 生产者
producer = KafkaProducer(bootstrap_servers=bootstrap_servers,
                         value_serializer=lambda v: json.dumps(v).encode('utf-8'))
# 发送消息到 Kafka
def send_message(message):
    producer.send(topic, value=message)
    producer.flush()


# 示例消息
sample_message = {"key": "value"}

# 发送示例消息到 Kafka
send_message(sample_message)

# 关闭生产者和消费者
producer.close()
